//
//  SRAlarmService.m
//  SportRing
//
//  Created by mykoma on 14/10/28.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRAlarmService.h"
#import "BlueToothLib.h"
#import "SRDB.h"

@implementation SRAlarmService

+(void)addObserverForBleData
{
    [[NSNotificationCenter defaultCenter] addObserverForName:NOTIFY_GET_ALARMS
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification *note)
     {
         const void * data = ((NSData *)note.object).bytes;
         for (int index = 0; index < 2; index ++) {
             SRAlarm * alarm0 = [SRAlarm findAlarmCINEForMe:[SRMe currentUser]
                                                    alarmId:index];
             [alarm0 configAttributesWithBlock:^(SRAlarm *aSelf) {
                 uint8_t flag;
                 memcpy(&flag, &data[index * 3 + 0], 1);
                 if (flag == 0x00) {
                     aSelf.on = @(NO);
                 }
                 else
                 {
                     aSelf.on = @(YES);
                     aSelf.sunday = @((flag & 0x01) != 0x00);
                     aSelf.monday = @((flag & 0x02) != 0x00);
                     aSelf.tuesday = @((flag & 0x04) != 0x00);
                     aSelf.wednesday = @((flag & 0x08) != 0x00);
                     aSelf.thursday = @((flag & 0x10) != 0x00);
                     aSelf.friday = @((flag & 0x20) != 0x00);
                     aSelf.saturday = @((flag & 0x40) != 0x00);
                     aSelf.once = @((flag & 0x80) != 0x00);
                 }
                 
                 uint16_t time;
                 memcpy(&time, &data[index * 3 + 1], 2);
                 aSelf.time = @(time);
             }];
         }
         [[NSNotificationCenter defaultCenter] postNotificationName:NEW_ALARM_REFRESH_NOTIFY object:nil];
     }];
}

+(RACSignal *)readAlarmsFromDevice
{
    RACSubject * subject = [RACSubject subject];
    
    [BlueToothLib alarmGetWithBlock:^(BOOL isTimeOut) {
        [subject sendNext:nil];
    }];
    
    return subject;
}

+(RACSignal *)updateAlarmToDevice:(SRAlarm *)alarm
{
    RACSubject * subject = [RACSubject subject];
    
    alarm_setting_t t;
    t.nID = alarm.alarmId.intValue == 0 ? 0x00 : 0x01;
    if (alarm.on.boolValue) {
        char sunday = alarm.sunday.boolValue ? 0x01 : 0x00;
        char monday = alarm.monday.boolValue ? 0x02 : 0x00;
        char tuesday = alarm.tuesday.boolValue ? 0x04 : 0x00;
        char wednesday = alarm.wednesday.boolValue ? 0x08 : 0x00;
        char thursday = alarm.thursday.boolValue ? 0x10 : 0x00;
        char friday = alarm.friday.boolValue ? 0x20 : 0x00;
        char saturday = alarm.saturday.boolValue ? 0x40 : 0x00;
        
        char once = alarm.once.boolValue ? 0x80 : 0x00;
        
        t.flag = sunday | monday | tuesday | wednesday | thursday | friday | saturday | once;
    }
    else
    {
        t.flag = 0x00;
    }
    t.minutes = alarm.time.intValue;
    
    [BlueToothLib alarmSetting:&t alarmCount:1 withBlock:^(BOOL isTimeOut) {
        [subject sendNext:nil];
    }];
    
    return subject;
}

@end
